import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:lib_uikit/resource/resources.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:lib_uikit/utils/gt_cache_manager.dart';

class GTNetworkImage extends StatelessWidget {
  /// 图片 URL
  final String? imageUrl;

  /// 占位图 widget
  final Widget? placeholderWidget;

  /// 占位图 URL。如果传入了 placeholderWidget，则会忽略 placeholder
  final String? placeholder;

  /// 图片适应方式
  final BoxFit fit;

  /// 占位图适应方式
  final BoxFit placeholderFit;

  /// 图片宽度
  final double? width;

  /// 图片高度
  final double? height;

  /// 图片对齐方式
  final Alignment alignment;

  /// 图片重复方式
  final ImageRepeat repeat;

  /// 是否匹配文本方向
  final bool matchTextDirection;

  /// HTTP 请求头
  final Map<String, String>? httpHeaders;

  /// 颜色
  final Color? color;

  /// 圆角半径
  final double? borderRadius;

  /// 图片缓存过期时间
  final GTImageCacheStalePeriod? stalePeriod;

  /// 是否隐藏图片加载动画
  final bool? hideImageAnimate;

  /// 将在内存中调整图像大小以具有特定宽度
  final int? cacheWidth;

  /// 将在内存中调整图像大小以具有特定高度
  final int? cacheHeight;

  /// 剪裁行为
  final Clip? clipBehavior;

  const GTNetworkImage(
    this.imageUrl, {
    this.fit = BoxFit.cover,
    this.placeholderWidget,
    this.placeholder,
    this.placeholderFit = BoxFit.cover,
    this.width,
    this.height,
    this.alignment = Alignment.center,
    this.repeat = ImageRepeat.noRepeat,
    this.matchTextDirection = false,
    this.httpHeaders,
    this.color,
    this.borderRadius,
    this.stalePeriod,
    this.hideImageAnimate = true,
    this.cacheWidth,
    this.cacheHeight,
    this.clipBehavior,
    super.key,
  });

  Widget buildPlaceholderWidget() {
    if (placeholderWidget != null) {
      return SizedBox(width: width, height: height, child: placeholderWidget);
    }
    if (placeholder == null) {
      return SvgPicture.asset(Images.uikitPlaceholder, width: width, height: height, fit: placeholderFit);
    }
    if (placeholder!.endsWith('svg')) {
      return SvgPicture.asset(placeholder!, width: width, height: height, fit: placeholderFit);
    }
    return Image.asset(placeholder!, width: width, height: height, fit: placeholderFit);
  }

  @override
  Widget build(BuildContext context) {
    late Widget content;
    if (imageUrl == null || imageUrl!.isEmpty) {
      content = buildPlaceholderWidget();
    } else if (!imageUrl!.startsWith('http')) {
      content = Image.asset(imageUrl!, width: width, height: height, fit: fit);
    } else {
      content = CachedNetworkImage(
          imageUrl: imageUrl!,
          placeholder: (BuildContext context, String url) {
            return buildPlaceholderWidget();
          },
          errorWidget: (BuildContext context, String url, Object? error) {
            return buildPlaceholderWidget();
          },
          fit: fit,
          width: width,
          height: height,
          memCacheWidth: cacheWidth,
          memCacheHeight: cacheHeight,
          alignment: alignment,
          repeat: repeat,
          matchTextDirection: matchTextDirection,
          httpHeaders: httpHeaders,
          color: color,
          cacheManager: GTCacheManager.getCacheManager(stalePeriod),
          fadeInDuration: hideImageAnimate == true ? Duration.zero : const Duration(milliseconds: 500),
          fadeOutDuration: hideImageAnimate == true ? Duration.zero : const Duration(milliseconds: 500));
    }
    if (borderRadius == null) {
      return content;
    } else {
      return ClipRRect(
        clipBehavior: clipBehavior ?? Clip.antiAlias,
        borderRadius: borderRadius == 0 ? BorderRadius.zero : BorderRadius.circular(borderRadius!),
        child: content,
      );
    }
  }
}
